package com.oa.lms.apps.dao.absence.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.lms.apps.dao.absence.IProcessesDao;
import com.oa.lms.apps.entity.absence.ProcessesValue;

public class ProcessesDaoImpl extends HibernateDaoSupport implements
		IProcessesDao {

	List<ProcessesValue> list;
	@Override
	public Integer add(ProcessesValue processesValue) {
		if (processesValue != null) {
			this.getHibernateTemplate().save(processesValue);
		}
		return processesValue.getProcessId();
	}

	@Override
	public void modify(ProcessesValue processesValue) {
		if (processesValue != null) {
			this.getHibernateTemplate().update(processesValue);
		}
	}

	@Override
	public ProcessesValue queryByProcessId(Integer processId) {
		if (processId != null) {
			return (ProcessesValue) this.getHibernateTemplate().get(ProcessesValue.class, processId);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProcessesValue> queryProcesses(final ProcessesValue processesValue) {
		String hqlStr = "FROM ProcessesValue pv WHERE 1=1";
		
		if (processesValue.getPostInfo() != null) {
			hqlStr += " AND pv.postInfo = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				
				if (processesValue.getPostInfo() != null) {
					query.setEntity(++index, processesValue.getPostInfo());
				}
				list = query.list();
				return list;
			}
		});
		return list;
	}

	public List<ProcessesValue> getList() {
		return list;
	}

	public void setList(List<ProcessesValue> list) {
		this.list = list;
	}


}
